package com.ruoyi.dataroom;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.framework.web.service.SysLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisDataInitializer implements CommandLineRunner {
    private final StringRedisTemplate redisTemplate;

    @Autowired
    private SysLoginService loginService;

    public RedisDataInitializer(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void run(String... args) throws Exception {
        LoginBody loginBody = new LoginBody();
        loginBody.setUsername("admin1");
        loginBody.setPassword("admin123");
        loginBody.setUuid("7a6e48e225c848a1bb960ae10c735e46");
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login1(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        redisTemplate.opsForValue().set("admin1", token);
        System.out.println(ajax);
    }

}
